package com.sita.ptcol.kksmod;

import com.google.common.io.BaseEncoding;
import com.google.common.primitives.Ints;
import com.google.common.primitives.Shorts;
import com.sita.ptcol.kksmod.parent.KksModParent;
import com.sita.ptcol.util.LatLngUtil;

/**
 * Created by zhebinwang on 2017/4/1.
 */
public class Ptcol19 extends KksModParent {

    private double lat;
    private double lng;
    private int rideflag;
    private String gpsid;

    public Ptcol19 paste(String hexmsg){
        //根据hexmsg文本来填充协议


        byte[] data = BaseEncoding.base16().decode(hexmsg);

        //协议第四位开始是数据
        int pos = 4;
        byte[] bytes = new byte[8];
        System.arraycopy(data, pos, bytes, 0, bytes.length);
        pos += bytes.length;
        gpsid = BaseEncoding.base16().encode(bytes);

        bytes = new byte[3];
        System.arraycopy(data, pos, bytes, 0, bytes.length);
        pos += bytes.length;
        //time = ProtocalUtil.shortTimeStringFromByteArray(bytes);
        //hhmmss = ProtocalUtil.timeStringToMillisUTC(time);

        //Long now = System.currentTimeMillis();
        //if (hhmmss > now + 60 * 60 * 1000 * 4L) {
        //    hhmmss = hhmmss - 60 * 60 * 1000 * 24L;
        //} else if (hhmmss < now - 60 * 60 * 1000 * 4L) {
        //    hhmmss = hhmmss + 60 * 60 * 1000 * 24L;
        //}

        bytes = new byte[4];
        System.arraycopy(data, pos, bytes, 0, bytes.length);
        pos += bytes.length;
        lat = LatLngUtil.fromDu(bytes);

        bytes = new byte[4];
        System.arraycopy(data, pos, bytes, 0, bytes.length);
        pos += bytes.length;
        lng = LatLngUtil.fromDu(bytes);

        bytes = new byte[2];
        System.arraycopy(data, pos, bytes, 0, bytes.length);
        pos += bytes.length;
        //altitude = Shorts.fromByteArray(bytes);

        //turn = data[pos] & 0xff;
        pos += 1;
        //brake = data[pos] & 0xff;
        pos += 1;
        //speed = data[pos] & 0xff;
        pos += 1;
        //骑行状态
        rideflag = data[pos] & 0xff;
        pos += 1;
        //soc = data[pos] & 0xff;
        pos += 1;
        //soh = data[pos] & 0xff;
        pos += 1;

        bytes = new byte[4];//总里程
        System.arraycopy(data, pos, bytes, 0, bytes.length);
        pos += bytes.length;
        //totalMileage = Ints.fromByteArray(bytes);

        bytes = new byte[4];//剩余里程数
        System.arraycopy(data, pos, bytes, 0, bytes.length);
        pos += bytes.length;
        //leftDistance = Ints.fromByteArray(bytes);

        bytes = new byte[2];//充放电次数
        System.arraycopy(data, pos, bytes, 0, bytes.length);
        pos += bytes.length;
        //chdisTimes = Shorts.fromByteArray(bytes);


        return this;
    }


    public double getLat() {
        return lat;
    }

    public Ptcol19 setLat(double lat) {
        this.lat = lat;
        return this;
    }

    public double getLng() {
        return lng;
    }

    public Ptcol19 setLng(double lng) {
        this.lng = lng;
        return this;
    }

    public int getRideflag() {
        return rideflag;
    }

    public Ptcol19 setRideflag(int rideflag) {
        this.rideflag = rideflag;
        return this;
    }

    public String getGpsid() {
        return gpsid;
    }

    public Ptcol19 setGpsid(String gpsid) {
        this.gpsid = gpsid;
        return this;
    }
/*
    //这个里面粘贴的是 冬哥的作品
    public void aaa(){
        int pos = 0;
        byte[] bytes = new byte[8];
        System.arraycopy(data, pos, bytes, 0, bytes.length);
        pos += bytes.length;
        terminalId = bytes;

        bytes = new byte[3];
        System.arraycopy(data, pos, bytes, 0, bytes.length);
        pos += bytes.length;
        time = ProtocalUtil.shortTimeStringFromByteArray(bytes);
        hhmmss = ProtocalUtil.timeStringToMillisUTC(time);

        Long now = System.currentTimeMillis();
        if (hhmmss > now + 60 * 60 * 1000 * 4L) {
            hhmmss = hhmmss - 60 * 60 * 1000 * 24L;
        } else if (hhmmss < now - 60 * 60 * 1000 * 4L) {
            hhmmss = hhmmss + 60 * 60 * 1000 * 24L;
        }

        bytes = new byte[4];
        System.arraycopy(data, pos, bytes, 0, bytes.length);
        pos += bytes.length;
        latitude = LatlngUtil.fromDu(bytes);

        bytes = new byte[4];
        System.arraycopy(data, pos, bytes, 0, bytes.length);
        pos += bytes.length;
        longitude = LatlngUtil.fromDu(bytes);

        bytes = new byte[2];
        System.arraycopy(data, pos, bytes, 0, bytes.length);
        pos += bytes.length;
        altitude = Shorts.fromByteArray(bytes);

        turn = data[pos] & 0xff;
        pos += 1;
        brake = data[pos] & 0xff;
        pos += 1;
        speed = data[pos] & 0xff;
        pos += 1;
        rideFlag = data[pos] & 0xff;
        pos += 1;
        soc = data[pos] & 0xff;
        pos += 1;
        soh = data[pos] & 0xff;
        pos += 1;

        bytes = new byte[4];//总里程
        System.arraycopy(data, pos, bytes, 0, bytes.length);
        pos += bytes.length;
        totalMileage = Ints.fromByteArray(bytes);

        bytes = new byte[4];//剩余里程数
        System.arraycopy(data, pos, bytes, 0, bytes.length);
        pos += bytes.length;
        leftDistance = Ints.fromByteArray(bytes);

        bytes = new byte[2];//充放电次数
        System.arraycopy(data, pos, bytes, 0, bytes.length);
        pos += bytes.length;
        chdisTimes = Shorts.fromByteArray(bytes);
    }*/
}
